﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

public partial class UserMasterPage1 : System.Web.UI.MasterPage
{
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
        if (Session["User"] != "")
        {
            btnLogOut.Visible = true;
            lblWelcome.Visible = true;
            hyViewCart.Visible = true;
            lblWelcome.Text = "Welcome " + Session["User"].ToString();
            txtUserNameLogin.Visible = false;
            txtPasswordLogin.Visible = false;
            btnLogin.Visible = false;
            btnRegisterLogin.Visible = false;
        }
        else
        {
            btnLogOut.Visible = false;
            lblWelcome.Visible = false;
            hyViewCart.Visible = false;
            txtUserNameLogin.Visible = true;
            txtPasswordLogin.Visible = true;
            btnLogin.Visible = true;
            btnRegisterLogin.Visible = true;
            lblWelcome.Visible = true;
            lblWelcome.Text = "You need to Login or Register";
            //Response.Redirect("Default.aspx");
        }
    }
    protected void checkUser(string username, string password)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetAccount", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@userName", SqlDbType.VarChar).Value = username;
            command.ExecuteNonQuery();
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
                lblWelcome.Visible = true;
                lblWelcome.Text = "User Name is not correct";
            }
            else if (ds.Tables[0].Rows[0][2].ToString() != password)
            {
                lblWelcome.Visible = true;
                lblWelcome.Text = "Password is not correct";
            }
            else
            {
                btnLogOut.Visible = true;
                lblWelcome.Text = "Welcome " + username;
                Session["User"] = username;
                hyViewCart.Visible = true;
                txtUserNameLogin.Visible = false;
                txtPasswordLogin.Visible = false;
                btnLogin.Visible = false;
                btnRegisterLogin.Visible = false;
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUserNameLogin.Text == "" && txtPasswordLogin.Text == "")
        {
            lblWelcome.Text = "Insert user name or password";
        }
        else if (txtUserNameLogin.Text == "")
        {
            lblWelcome.Text = "Insert user name";
        }
        else if (txtPasswordLogin.Text == "")
        {
            lblWelcome.Text = "Insert password";
        }
        else
        {
            checkUser(txtUserNameLogin.Text, txtPasswordLogin.Text);
        }
    }
    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        Session["User"] = "";
        btnLogOut.Visible = false;
        lblWelcome.Visible = false;
        hyViewCart.Visible = false;
        txtUserNameLogin.Visible = true;
        txtPasswordLogin.Visible = true;
        btnLogin.Visible = true;
        btnRegisterLogin.Visible = true;
    }
    protected void btnRegisterLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("RegisterAccount.aspx");
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("https://www.google.com.vn/");
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("https://www.facebook.com/");
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("http://edition.cnn.com/");
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("https://twitter.com/");
    }
    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("https://youtube.com/");
    }
}
